﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class User_ShortMessageShow : System.Web.UI.Page
{
    private PagedDataSource pds = null;
    private string type = "";
    private int days = 0;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //string curUserLoginName = "";
        //if (Request["uln"] != null)
        //{
        //    curUserLoginName = Request["uln"].Trim();
        //}
        if (!Page.IsPostBack)
        {
            if (Request["type"] != null)
            {
                type = Request["type"];
            }
            else
            {
                type = "incept";
            }
            this.NewPagedDataSource();
            dataListDataBind();
            ViewState["pg_in"] = pds.CurrentPageIndex;
            ViewState["type"] = type;
            ViewState["days"] = days;
        }
    }

    private void NewPagedDataSource()
    {
        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = 8;
        this.findShortMessageAndDataBind(type, days);
    }

    protected void dataListDataBind()
    {
        this.DataList1.DataSource = pds;
        DataList1.DataBind();
    }

    //查询短消息绑定
    protected void findShortMessageAndDataBind(string type, int findDate)
    {
        User curLoginUser = (User)Session["user"];
        string uln = curLoginUser.LoginName;
        SectionEmpMsgtbService sectionEmpMsgtbService = new SectionEmpMsgtbService();
        List<SectionEmpMsgtb> sectionEmpMsgs = null;
        if ("incept".Equals(type))
        {
            sectionEmpMsgs = sectionEmpMsgtbService.findByCurUserLoginName(uln);
        }
        else
        {
            sectionEmpMsgs = sectionEmpMsgtbService.findSendMsgsByUserLoginName(uln);
        }
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add(new DataColumn("MsgId", typeof(Int32)));
        dataTable.Columns.Add(new DataColumn("MsgContent", typeof(string)));
        dataTable.Columns.Add(new DataColumn("SendUserName", typeof(string)));
        dataTable.Columns.Add(new DataColumn("SendDateTime", typeof(string)));
        dataTable.Columns.Add(new DataColumn("isNew", typeof(string)));
        dataTable.Columns.Add(new DataColumn("InceptUserName", typeof(string)));
        foreach (SectionEmpMsgtb sectionEmpMsg in sectionEmpMsgs)
        {
            DataRow dr = dataTable.NewRow();
            if (findDate == 0)
            {
                dr["MsgId"] = sectionEmpMsg.EmpMsgId;
                string msgContent = sectionEmpMsg.MsgContent;
                if (msgContent.Length > 30)
                {
                    msgContent = msgContent.Substring(0, 30);
                }
                string msgState = "";
                if (sectionEmpMsg.ReadState == 0)
                {
                    msgState = "New";
                }
                dr["MsgContent"] = msgContent;
                dr["SendUserName"] = sectionEmpMsg.SendUserName;
                dr["SendDateTime"] = sectionEmpMsg.SendDateTime.ToString();
                dr["isNew"] = msgState;
                dr["InceptUserName"] = sectionEmpMsg.InceptUserName;
            }
            else
            {
                DateTime lastTime = DateTime.Now.AddDays(-findDate);
                if (sectionEmpMsg.SendDateTime > lastTime)
                {
                    dr["MsgId"] = sectionEmpMsg.EmpMsgId;
                    string msgContent = sectionEmpMsg.MsgContent;
                    if (msgContent.Length > 30)
                    {
                        msgContent = msgContent.Substring(0, 30);
                    }
                    string msgState = "";
                    if (sectionEmpMsg.ReadState == 0)
                    {
                        msgState = "New";
                    }
                    dr["MsgContent"] = msgContent;
                    dr["SendUserName"] = sectionEmpMsg.SendUser.UserName;
                    dr["SendDateTime"] = sectionEmpMsg.SendDateTime.ToString();
                    dr["isNew"] = msgState;
                    dr["InceptUserName"] = sectionEmpMsg.InceptUserName;
                }
                else
                {
                    dr = null;
                }
            }
            if (dr != null)
            {
                dataTable.Rows.Add(dr);
            }
        }
        DataSet ds = new DataSet();
        ds.Tables.Add(dataTable);
        pds.DataSource = ds.Tables[0].DefaultView;
    }
    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if ("incept".Equals(this.type))
            {
               // ((Label)e.Item.FindControl("lblSendOrInceptUser")).Text = "发送人:";
                ((Label)e.Item.FindControl("lblSendUserName")).Visible = true;
                ((Label)e.Item.FindControl("lblInceptUserName")).Visible = false;
            }
            else
            {
                //((Label)e.Item.FindControl("lblSendOrInceptUser")).Text = "接收人:";
                ((Label)e.Item.FindControl("lblInceptUserName")).Visible = true;
                ((Label)e.Item.FindControl("lblSendUserName")).Visible = false;
                ((Label)e.Item.FindControl("lblMessageState")).Visible = false;
            }
            ((HyperLink)e.Item.FindControl("hplMsg")).NavigateUrl = "MsgContentShow.aspx?mid=" +
                ((HtmlInputHidden)e.Item.FindControl("MsgId")).Value + "&type=" + type;
        }
        if(e.Item.ItemType == ListItemType.Footer)
        {
            if (pds.IsFirstPage)
            {
                ((Button)e.Item.FindControl("btnRetu")).Enabled = false;
                ((Button)e.Item.FindControl("btnGoTop")).Enabled = false;
            }
            if (pds.IsLastPage)
            {
                ((Button)e.Item.FindControl("btnNext")).Enabled = false;
                ((Button)e.Item.FindControl("htnGoLast")).Enabled = false;
            }
            ((Label)e.Item.FindControl("lblPageState")).Text = "当前第<font color='red'>" + (pds.CurrentPageIndex + 1) + "</font>页/共<font color='red'>" + pds.PageCount + "</font>页";
        }
    }

    protected void btnRetu_Click(object sender, EventArgs e)
    {
        if (ViewState["type"] != null && ViewState["days"] != null)
        {
            this.type = (string)ViewState["type"];
            this.days = (int)ViewState["days"];
        }
        this.NewPagedDataSource();
        if (ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int)ViewState["pg_in"];
        }
        pds.CurrentPageIndex--;
        this.dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["type"] = type;
        ViewState["days"] = days;
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        if (ViewState["type"] != null && ViewState["days"] != null)
        {
            this.type = (string)ViewState["type"];
            this.days = (int)ViewState["days"];
        }
        this.NewPagedDataSource();
        if (ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int)ViewState["pg_in"];
        }
        pds.CurrentPageIndex++;
        this.dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["type"] = type;
        ViewState["days"] = days;
    }
    protected void btnDelMsg_Click(object sender, EventArgs e)
    {
        if (ViewState["type"] != null && ViewState["days"] != null)
        {
            this.type = (string)ViewState["type"];
            this.days = (int)ViewState["days"];
        }
        string[] mids = Request.Params.GetValues("chkCur");
        int[] msgIds = null;
        if (mids != null && mids.Length > 0)
        {
            msgIds = new int[mids.Length];
            for (int i = 0; i < mids.Length; i++)
            {
                msgIds[i] = int.Parse(mids[i]);
            }
        }
        SectionEmpMsgtbService sectionEmpMsgtbService = new SectionEmpMsgtbService();
        sectionEmpMsgtbService.delMsgByMsgId(msgIds, type);
        this.NewPagedDataSource();
        if (ViewState["pg_in"] != null)
        {
            int pg = (int)ViewState["pg_in"];
            if (pg == pds.PageCount)
            {
                pds.CurrentPageIndex = pg - 1;
            }
            else
            {
                pds.CurrentPageIndex = pg;
            }
        }
        this.dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["type"] = type;
        ViewState["days"] = days;
    }
    protected void btnGoTop_Click(object sender, EventArgs e)
    {
        if (ViewState["type"] != null && ViewState["days"] != null)
        {
            this.type = (string)ViewState["type"];
            this.days = (int)ViewState["days"];
        }
        this.NewPagedDataSource();
        //if (ViewState["pg_in"] != null)
        //{
        //    pds.CurrentPageIndex = (int)ViewState["pg_in"];
        //}
        pds.CurrentPageIndex = 0;
        this.dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["type"] = type;
        ViewState["days"] = days;
    }
    protected void htnGoLast_Click(object sender, EventArgs e)
    {
        if (ViewState["type"] != null && ViewState["days"] != null)
        {
            this.type = (string)ViewState["type"];
            this.days = (int)ViewState["days"];
        }
        this.NewPagedDataSource();
        pds.CurrentPageIndex = pds.PageCount - 1;
        this.dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["type"] = type;
        ViewState["days"] = days;
    }
    protected void ddlFindDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ViewState["type"] != null && ViewState["days"] != null)
        {
            this.type = (string)ViewState["type"];
            this.days = (int)ViewState["days"];
        }
        //this.days = int.Parse(this.ddlFindDate.SelectedItem.Value);
        this.NewPagedDataSource();
        this.dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["type"] = type;
        ViewState["days"] = days;
    }
}
